package com.kt.uokos.yimo.wx.controller;

import com.kt.uokos.yimo.anno.validator.Order;
import com.kt.uokos.yimo.anno.validator.Sort;
import com.kt.uokos.yimo.dao.Collect;
import com.kt.uokos.yimo.dao.Product;
import com.kt.uokos.yimo.dao.Topic;
import com.kt.uokos.yimo.service.CollectService;
import com.kt.uokos.yimo.service.ProductService;
import com.kt.uokos.yimo.service.TopicService;
import com.kt.uokos.yimo.util.JacksonUtil;
import com.kt.uokos.yimo.util.ResponseUtil;
import com.kt.uokos.yimo.wx.annocation.LoginUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: TODO
 * @className: WxCollectController.java
 * @author: Kang Hao
 * @date: 2022/4/23 20:37
 **/
@RestController
@RequestMapping("/wx/collect")
public class WxCollectController {
    @Resource
    private CollectService collectService;

    @Resource
    private TopicService topicService;

    @Resource
    private ProductService productService;


    @GetMapping("list")
    public Object list(@LoginUser Integer userId,
                       @NotNull Byte type,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        if (userId == null) {
            return ResponseUtil.unlogin();
        }
        List<Collect> collectList = collectService.queryByType(userId, type, page, limit, sort, order);

        List<Object> collects = new ArrayList<>(collectList.size());
        for (Collect collect : collectList) {
            Map<String, Object> c = new HashMap();
            c.put("id", collect.getId());
            c.put("type", collect.getType());
            c.put("valueId", collect.getValueId());
            if (type == (byte)0){
                //查询商品相关信息
                Product product = productService.queryByProId(collect.getValueId());
                c.put("name", product.getName());
                c.put("brief", product.getBrief());
                c.put("picUrl", product.getPicUrl());
                c.put("retailPrice", product.getRetailPrice());
            } else {
                //查询专题相关信息
                Topic topic = topicService.findById(collect.getValueId());
                c.put("title", topic.getTitle());
                c.put("subtitle", topic.getTitle());
                c.put("price", topic.getPrice());
                c.put("picUrl", topic.getPicUrl());
            }
            collects.add(c);
        }

        return ResponseUtil.okList(collects, collectList);
    }




    @PostMapping("/addordelete")
    public Object addordelete(@LoginUser Integer userId, @RequestBody String body){
        //根据其查询表中数据来决定是否是添加收藏还是取消收藏
        if (userId==null){
            return ResponseUtil.unlogin();
        }
        Byte type = JacksonUtil.parseByte(body, "type");
        Integer valueId = JacksonUtil.parseInteger(body, "valueId");

        Collect collect = collectService.queryByTypeAndValueId(userId, type, valueId);

        if (collect != null){
            collectService.deleteById(collect.getId());
        }else {
            collect = new Collect();
            collect.setUserId(userId);
            collect.setValueId(valueId);
            collect.setType(type);
            collectService.add(collect);
        }

        return ResponseUtil.ok();
    }

}
